<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Class : Login_model (Login Model)
 * Login model class to get to authenticate user credentials 
 * @author : Kishor Mali
 * @version : 1.1
 * @since : 15 November 2016
 */
class Goods_model extends CI_Model
{
    function getAll($page, $segment) {
    	$this->db->select('BaseTbl.*');
        $this->db->from('tbl_goods as BaseTbl');
        // $this->db->order_by('BaseTbl.tbl_goods', 'ASC');
        $this->db->limit($page, $segment);
        $query = $this->db->get();
        
        $result = $query->result();        
        return $result;
    }

    function get($goods_id) {
        $this->db->select('*');
        $this->db->from('tbl_goods as BaseTbl');
        $this->db->where('BaseTbl.id', $goods_id);
        $query = $this->db->get();
        $result = $query->row();
        return $result ? $result : $this->emptyModel();
    }

    function create($goods){
    	$this->db->insert('tbl_goods', $goods);
    	return $this->db->insert_id();
    }

    function delete($id) {
    	$this->db->delete('tbl_goods', array('id' => $id));
    	return TRUE;
    }

    function update($id, $goods){
    	$this->db->where('id', $id);
    	$this->db->update('tbl_goods', $goods);
    	return TRUE;
    }

    function batch($goodsGroup){
    	$this->db->insert_batch('tbl_goods', $goodsGroup);
    	return TRUE;
    }

    function updateOrCreate($goods) {

        if(isset($goods['id'])) {
            $this->update($goods['id'], $goods);
        } else {
            $this->create($goods);
        }
        return TRUE;
    }

    function truncate(){
    	$this->db->from('tbl_goods');
		$this->db->truncate();
		return TRUE;
    }

    function emptyModel(){
        return (Object) array(
            'spec_no'=>'',
            'goods_name'=>'',
            'price'=>'',
            'gift_length'=>'',
            'gift_width'=>'',
            'gift_height'=>'',
            'box_length'=>'',
            'box_width'=>'',
            'box_height'=>'',
            'piece_weight'=>'',
            'package_weight'=>'',
            'package_count'=>'',
        );
    }
}

?>